﻿using System;
using System.Collections.Generic;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;

namespace SunrayEngine.Maps
{
    [Serializable]
    public class MapTile
    {
        #region fields
        protected Point offset;
        protected Texture2D texture;
        protected Texture2D walkabelTexture;
        protected PixelInfo[,] pixels;
        protected Rectangle bounds;
        protected Rectangle transformedBounds;
        #endregion

        #region properties
        public MapGrid Grid { get { return Grid; } }
        public Point Offset { get { return offset; } }
        public Rectangle Bounds { get { return transformedBounds; } }
        public PixelInfo[,] Pixels { get { return pixels; } }
        #endregion

        #region initialize
        public MapTile(int x, int y, int width, int height, GraphicsDevice device)
        {
            offset = Point.Zero;
            texture = new Texture2D(device, width, height);
            walkabelTexture = new Texture2D(device, width, height);
            bounds = new Rectangle(x, y, width, height);
            transformedBounds = bounds;
            pixels = new PixelInfo[width, height];

            for (int i = 0; i < width; i++)
                for (int j = 0; j < height; j++)
                    pixels[i, j].Color = Color.White;

            PixelsToTexture();
            WalkabelToTexture();

        }
        #endregion

        #region methods
        public void SetPixelsFromTexture(Texture2D tex)
        {
            if (tex.Width > bounds.Width || tex.Height > bounds.Height)
                throw new Exception("Invalid texture size!");

            Color[] texCols = new Color[tex.Width * tex.Height];
            tex.GetData(texCols);
            for (int y = 0; y < tex.Height; y++)
                for (int x = 0; x < tex.Width; x++)
                    pixels[x, y ].Color = texCols[x + tex.Width * y];

            PixelsToTexture();
        }

        public void EditOffset(Point p)
        {
            offset.X += p.X;
            offset.Y += p.Y;
            transformedBounds = new Rectangle(bounds.X + offset.X, bounds.Y + offset.Y, bounds.Width, bounds.Height);
        }

        public void WalkabelToTexture()
        {
            List<Color> colors = new List<Color>();

            for (int x = 0; x < bounds.Height; x++)
                for (int y = 0; y < bounds.Width; y++)
                    if (pixels[y, x].WalkAbel)
                        colors.Add(Color.Red * .5f);
                    else
                        colors.Add(new Color(0, 0, 0, 0));

            walkabelTexture.SetData(colors.ToArray());
        }

        public void PixelsToTexture()
        {
            List<Color> colors = new List<Color>();

            for (int x = 0; x < bounds.Height; x++)
                for (int y = 0; y < bounds.Width; y++)
                    colors.Add(pixels[y, x].Color);

            texture.SetData(colors.ToArray());
        }

        public void EditInfo(PixelInfo[,] pixels)
        {
            this.pixels = pixels;
            PixelsToTexture();
        }

        public void Draw(SpriteBatch spriteBatch)
        {
            if(texture != null)
                spriteBatch.Draw(texture, transformedBounds, Color.White);
        }

        public void DrawCollMap(SpriteBatch spriteBatch)
        {
            if (texture != null)
                spriteBatch.Draw(walkabelTexture, transformedBounds, Color.White);
        }
        #endregion
    }
}
